import 'package:changxindianzishangwu/pages/my/settings/test.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

class ImageTest extends StatefulWidget {
  ImageTest({Key key}) : super(key: key);
  PermissionStatus status;
  @override
  _ImageTestState createState() => _ImageTestState();
}

class _ImageTestState extends State<ImageTest> {
  PickedFile _imgPath;
  final _picker = ImagePicker();

  @override
  void initState() {
    super.initState();
  } // 获取图片方法

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text("ImagePicker"),
            ),
            body: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  _ImageView(_imgPath),
                  RaisedButton(
                    onPressed: _takePhoto,
                    child: Text("拍照"),
                  ),
                  RaisedButton(
                    onPressed: _openGallery,
                    child: Text("选择照片"),
                  ),
                  InkWell(
                    onTap: () {
                      Get.to(BottomSheetDemo());
                    },
                    child: Text("123"),
                  )
                ],
              ),
            )));
  }

  /*图片控件*/
  Widget _ImageView(PickedFile imgPath) {
    if (imgPath == null) {
      return Center(
        child: Text("请选择图片或拍照"),
      );
    } else {
      return Image.asset(
        imgPath.path,
      );
    }
  }

  /*拍照*/
  _takePhoto() async {
    var image = await _picker.getImage(source: ImageSource.camera);

    setState(() {
      _imgPath = image;
    });
  }

  /*相册*/
  _openGallery() async {
    await _handleCameraAndMic();
    var image = await _picker.getImage(
        source: ImageSource.gallery,
        maxHeight: 100,
        maxWidth: 100,
        imageQuality: 30);
    setState(() {
      _imgPath = image;
    });
  }

  _handleCameraAndMic() async {
    // 申请权限

    bool a = await await Permission.photos.request().isGranted;
    print(a);
    if (a == true) {
      print(a);
      print("已经打开");
    } else {
      showToast("请打开图库权限");
      await Future.delayed(Duration(seconds: 1));
      print(a);
      openAppSettings();
    }

//
  }
}
